﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace MN.Framework.Extensions
{
    public static class Generic
    {
        public static IEnumerable<object> WhereByLike(this IEnumerable<object> source, string query)
        {
            query = query.Replace("(", string.Empty).Replace(")", string.Empty);
            var columnName = query.Substring(0, query.IndexOfAnyCase("Like"));
            var propertyValue = query.Substring(query.IndexOfAnyCase("Like") + 4, query.Length - query.IndexOfAnyCase("Like") - 4);
            propertyValue = propertyValue.Replace("%", "");
            return source.Where(m => { return m.GetType().GetProperty(columnName).GetValue(m, null).ToString().StartsWith(propertyValue); });
        }
        public static IEnumerable<object> WhereQuery(this IEnumerable<object> source, string columnName, string propertyValue)
        {
            return source.Where(m => { return m.GetType().GetProperty(columnName).GetValue(m, null).ToString().StartsWith(propertyValue); });

        }
        public static int IndexOfAnyCase(this string source, string value)
        {
            return source.ToLower().IndexOf(value.ToLower());
        }
    }
}
